package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.City;
import cn.kgc.springcloud.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 课工场
 * @since 2023/9/11
 */
@RestController
@RequestMapping("city")
@Slf4j
public class CityController {

    @Autowired
    private CityService cityService;

    // 获取城市列表
    @GetMapping("list")
    public List<City>  cityList(){
        List<City> list = cityService.list();
        return list;
    }

    //添加城市信息
    @PostMapping("add")
    public City  addCity(@RequestBody City city){

        log.info("before add city ={}",city);

        //城市信息添加完成后 city实体类中已经有了id
        cityService.save(city);

        log.info("after add city ={}",city);

        return city;
    }


    // 根据id  获取城市信息
    @GetMapping("getById/{id}")
    public City  getById(@PathVariable("id") Integer id){
        return cityService.getById(id);
    }

}
